package com.hmyd.hmyd_android.view.ad;

import com.hmyd.hmyd_android.R;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;


/**
 * 引用圆点
 **/
public class PointView extends View {

	private int mCurrentPage = -1;
	private int mTotalPage = 0;
	private int mWidth = 0;
	private int mHeight = 0;
	private int mSplace = 0;
	private int mNormalID = 0;// 正常图片
	private int mPressedID = 0;// 点击图片
	private int mType = CENTER;
	public int layoutWidth = 0;

	public final static int CENTER = 0;
	public final static int LEFT = 1;
	public final static int RIGHT = 2;

	public PointView(Context context) {
		this(context, null);
	}

	public PointView(Context context, AttributeSet attrs) {
		this(context, attrs, R.style.DivPointView);
	}

	public PointView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		final Resources res = getResources();
		final int defaultWidth = res
				.getDimensionPixelSize(R.dimen.point_width_default);
		final int defaultHeight = res
				.getDimensionPixelSize(R.dimen.point_height_default);
		final int defaultSplace = res
				.getDimensionPixelSize(R.dimen.point_splace_default);
		final int defaultNormal = R.drawable.view_point;
		final int defaultPressed = R.drawable.view_point_pressed;

		// Retrieve styles attributes
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.PointView, defStyle, 0);
		mWidth = a.getDimensionPixelSize(R.styleable.PointView_point_width,
				defaultWidth);
		mHeight = a.getDimensionPixelSize(R.styleable.PointView_point_height,
				defaultHeight);
		mSplace = a.getDimensionPixelSize(R.styleable.PointView_point_splace,
				defaultSplace);
		mNormalID = a.getResourceId(R.styleable.PointView_point, defaultNormal);
		mPressedID = a.getResourceId(R.styleable.PointView_point_pressed,
				defaultPressed);
		a.recycle();

	}

	public void setTotalPage(int nPageNum) {
		mTotalPage = nPageNum;
		if (mCurrentPage >= mTotalPage)
			mCurrentPage = mTotalPage - 1;
	}

	public int getCurrentPage() {
		return mCurrentPage;
	}

	public void setCurrentPage(int nPageIndex) {
		if (nPageIndex < 0 || nPageIndex >= mTotalPage)
			return;

		if (mCurrentPage != nPageIndex) {
			mCurrentPage = nPageIndex;
			this.invalidate();
		}
	}

	public void setGravity(int type) {
		mType = type;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		int width;

		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		int height;
		
		if (widthMode == MeasureSpec.EXACTLY) {
			width = widthSize;
		} else {
			width = mWidth * mTotalPage + mSplace * (mTotalPage - 1);
		}

		if (heightMode == MeasureSpec.EXACTLY) {
			height = heightSize;
		} else {
			height = mHeight;
		}
		setMeasuredDimension(width, height);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG
				| Paint.FILTER_BITMAP_FLAG);
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.BLACK);

		Rect r = new Rect();

		this.getDrawingRect(r);
		int x = 0;
		int y = 0;

		layoutWidth = mWidth * mTotalPage + mSplace * (mTotalPage - 1);

		switch (mType) {
		case CENTER:
			x = (r.width() - (mWidth * mTotalPage + mSplace * (mTotalPage - 1))) / 2;
			break;
		case LEFT:
			x = r.width();
			break;
		case RIGHT:
			x = r.width() - (mWidth * mTotalPage + mSplace * (mTotalPage - 1));
			break;
		}

		y = (r.height() - mHeight) / 2;

		for (int i = 0; i < mTotalPage; i++) {
			int resid = mNormalID;
			if (i == mCurrentPage) {
				resid = mPressedID;
			}
			Rect r1 = new Rect();
			r1.left = x;
			r1.top = y;
			r1.right = x + mWidth;
			r1.bottom = y + mHeight;

			Bitmap bmp = BitmapFactory.decodeResource(getResources(), resid);
			canvas.drawBitmap(bmp, null, r1, paint);
			x += mWidth + mSplace;
		}

	}

	public void DrawImage(Canvas canvas, Bitmap mBitmap, int x, int y, int w,
			int h, int bx, int by) {
		Rect src = new Rect();// 图片裁剪区域
		Rect dst = new Rect();// 屏幕裁剪区域
		src.left = bx;
		src.top = by;
		src.right = bx + w;
		src.bottom = by + h;

		dst.left = x;
		dst.top = y;
		dst.right = x + w;
		dst.bottom = y + h;

		// canvas.drawBitmap(mBitmap, src, dst, mPaint);
		src = null;
		dst = null;
	}

}
